package com.project2.service;

import com.github.pagehelper.PageInfo;
import com.project2.bean.PageBo;
import com.project2.bean.bo.WxCollectAddOrDeleteBo;
import com.project2.bean.po.*;
import com.project2.bean.vo.WxCollectVo;
import com.project2.bean.vo.WxShowCollectListVo;
import com.project2.mapper.MarketCollectMapper;
import com.project2.mapper.MarketGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: MH
 * @Date: 2022-08-15 10:06
 * @Description: 前台商品和专题收藏的具体实现
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    /**
    *@Author: MH
    *@date: 2022/8/15 20:24
    *@Description:收藏和取消收藏的具体实现,先查询收藏表中是否有这个商品的收藏历史,
     *             如果没有,说明这个商品从来没有收藏过,那么就需要插入数据.如果根据valueId
     *             查到了该收藏记录,说明表中已经存过这些信息了,只需要将deleted状态改成相反的即可
    */
    @Override
    public Integer addOrDeleteCollect(WxCollectAddOrDeleteBo addOrDeleteBo, HttpServletRequest httpServletRequest) {
        String header = httpServletRequest.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) httpServletRequest.getSession().getAttribute(header);
        if (user == null) {
            return null;
        }
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andTypeEqualTo(addOrDeleteBo.getType())
                .andValueIdEqualTo(addOrDeleteBo.getValueId());
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        Date date = new Date();
        // 如果根据valueId和type没有查出来数据,说明这个商品或者专题从未被收藏过,那么就需要新建数据
        if (marketCollects.size() == 0) {
            MarketCollect marketCollect = new MarketCollect();
            marketCollect.setType(addOrDeleteBo.getType());
            marketCollect.setValueId(addOrDeleteBo.getValueId());
            marketCollect.setAddTime(date);
            marketCollect.setUpdateTime(date);
            marketCollect.setUserId(user.getId());
            marketCollect.setDeleted(false);
            int insert = marketCollectMapper.insert(marketCollect);
            return insert;
        }
        // 走到这说明根据valueId和type查出来数据了,那么只要判断它的deleted状态,
        // 然后改成相反状态,然后更新update时间即可
        for (MarketCollect marketCollect : marketCollects) {
            Boolean deleted = marketCollect.getDeleted();
            marketCollect.setUpdateTime(date);
            marketCollect.setDeleted(!deleted);
            marketCollectMapper.updateByExampleSelective(marketCollect, marketCollectExample);
        }
        return 200;
    }

    @Override
    public WxShowCollectListVo showCollect(Byte type, PageBo pageBo) {
        List<WxCollectVo> wxCollectVos = new ArrayList<>();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andTypeEqualTo(type)
                .andDeletedEqualTo(false);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        for (MarketCollect marketCollect : marketCollects) {
            WxCollectVo wxCollectVo = new WxCollectVo();
            wxCollectVo.setId(marketCollect.getId());
            wxCollectVo.setValueId(marketCollect.getValueId());
            wxCollectVo.setType(type);
            criteria1.andIdEqualTo(marketCollect.getValueId());
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(marketCollect.getValueId());
            wxCollectVo.setBrief(marketGoods.getBrief());
            wxCollectVo.setName(marketGoods.getName());
            wxCollectVo.setPicUrl(marketGoods.getPicUrl());
            wxCollectVo.setRetailPrice(marketGoods.getRetailPrice());
            wxCollectVos.add(wxCollectVo);
        }
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(marketCollects);
        WxShowCollectListVo wxShowCollectListVo = new WxShowCollectListVo();
        wxShowCollectListVo.setLimit(pageBo.getLimit());
        wxShowCollectListVo.setPage(pageBo.getPage());
        wxShowCollectListVo.setPages(marketCollectPageInfo.getPages());
        wxShowCollectListVo.setTotal(marketCollectPageInfo.getSize());
        wxShowCollectListVo.setList(wxCollectVos);
        return wxShowCollectListVo;
    }

}
